package com.newppjoke.libnetwork;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Project Name: ktnewppjoke
 * File Name:    AipService.java
 * ClassName:    AipService
 *
 * Description: TODO.
 *
 * @author My start
 * @date 2022年10月17日 5:23 下午
 *
 * Copyright (c) 2022年,  new Network CO.ltd. All Rights Reserved.
 */
public class AipService
{

    public static OkHttpClient okHttpClient;

    static {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        okHttpClient = new OkHttpClient().newBuilder()
                                                      .readTimeout(5, TimeUnit.SECONDS)
                                                      .writeTimeout(5, TimeUnit.SECONDS)
                                                      .callTimeout(5, TimeUnit.SECONDS)
                                                      .addInterceptor(interceptor)
                                                      .build();
        //http 证书问题
        TrustManager [] trustManagers =new TrustManager[]{new X509TrustManager()
        {
            @Override
            public void checkClientTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException
            {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain,
                                           String authType) throws CertificateException
            {

            }

            @Override
            public X509Certificate[] getAcceptedIssuers()
            {
                return new X509Certificate[0];
            }
        }};
        try
        {
            SSLContext ssl = SSLContext.getInstance("SSL");
            ssl.init(null,trustManagers,new SecureRandom());

            HttpsURLConnection.setDefaultSSLSocketFactory(ssl.getSocketFactory());

            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier()
            {
                @Override
                public boolean verify(String hostname, SSLSession session)
                {
                    return true;
                }
            });


        }
        catch (NoSuchAlgorithmException | KeyManagementException e)
        {
            e.printStackTrace();
        }

    }



}
